package mytime

import (
	"time"
)

const timeFormat1  = "2006-01-02 15:04:05"
var myLocation *time.Location
func init() {
	myLocation=parseLocation("Asia/Shanghai")
}



/**
得到形如1586966620的时间
 */
func UnixTime()  int64 {

	return time.Now().Unix()
}

/**
1586966620--->"2006-01-02 15:04:05"
 */
func FromUnixTime(t int64) string {
	return time.Unix(t, 0).Format(timeFormat1)
}
/**
"2006-01-02 15:04:05"东8区的时间--->1586966620,
 */
func FromStringTime(t string) int64 {

	tt,err:=time.ParseInLocation(timeFormat1,t,myLocation)
	if err != nil {
		return 0
	}
	return tt.Unix()
}
/**
"2006-01-02 15:04:05"+(时，分，秒）->"2006-01-02 15:04:05"
//增加年月日，负数为减
 */
func TimeAdd1(sc string,years,months,days int) string {
	tt,err:=time.ParseInLocation(timeFormat1,sc,myLocation)
	if err != nil {
		return sc
	}
	tn:=tt.AddDate(years, months, days)
	return tn.Format(timeFormat1)
}
//增加时分秒，负数为减
func TimeAdd2(sc string,hours,minutes,seconds time.Duration) string {
	tt,err:=time.ParseInLocation(timeFormat1,sc,myLocation)
	if err != nil {
		return sc
	}
	tn:=tt.Add(hours*time.Hour+minutes*time.Minute+seconds*time.Second)
	return tn.Format(timeFormat1)
}

func parseLocation(zoneName string)  (l *time.Location)  {
	var err error
	if l, err = time.LoadLocation(zoneName); err != nil {
		l,_=time.LoadLocation("Asia/Shanghai")
		return
	}
	return
}
